/**
 * @file hal_adapter.h
 * @brief 
 * @author  ()
 * @version 1.0
 * @date 2024-09-13
 * @ingroup bsp
 * 
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-13 <td>1.0     <td>     <td>内容
 * </table>
 */
#ifndef __HAL_ADAPTER_H
#define __HAL_ADAPTER_H

/* includes ---------------------------------------------------------------------------------------------------------*/
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>



/**
 *  @brief   bsp部分板级配置项
 *  @{
 */


#ifdef __cplusplus
extern "C" {
#endif


#ifndef _SSIZE_T
#define _SSIZE_T
typedef signed int ssize_t;
#endif


#define DDL_ZERO_STRUCT(x)         memset(&x, 0, sizeof(x));


#ifdef STM32U575xx
#include "stm32u575xx.h"
#include "stm32u5xx_hal.h"
#include "stm32u5xx_hal_uart.h"
#include "stm32u5xx_hal_usart.h"
#include "stm32u5xx_hal_uart_ex.h"
#include "stm32u5xx_hal_usart_ex.h"
#include "stm32u5xx_hal_flash.h"
#include "stm32u5xx_hal_flash_ex.h"
#include "stm32u5xx_hal_spi_ex.h"
#include "stm32u5xx_hal_spi.h"
#include "stm32u5xx_hal_def.h"
#include "stm32u5xx_hal_rcc.h"
#include "stm32u5xx_hal_iwdg.h"
#include "stm32u5xx_hal_adc.h"
#include "stm32u5xx_hal_rtc.h"


/* co-mcu comm port */
#define comm_uart_id                (4)
#define comm_uart_ctx          (huart4)      /* COMM HOST */
extern  UART_HandleTypeDef      huart4;
#define COMM_UART_PERIHERAL    (UART4)
#define COMM_IRQn               UART4_IRQn
#define COMM_UART_TX_PIN			GPIO_PIN_0  
#define COMM_UART_RX_PIN			GPIO_PIN_1  
#define COMM_UART_PORT			    GPIOA    
#define COMM_UART_PORT_AF           GPIO_AF8_UART4
#define comm_gpioclk_enable()  do{\
                                    __HAL_RCC_GPIOA_CLK_ENABLE();\
                                }while(0)
#define comm_uartclk_enable()  do{\
                                    RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};\
                                    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_UART4;\
                                    PeriphClkInitStruct.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;\
                                    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { \
                                        Error_Handler();\
                                    }\
                                    __HAL_RCC_UART4_CLK_ENABLE();\
                                }while(0)                                

/* debug port */
#define debug_uart_id               (3)
#define debug_uart_ctx          (huart3)     /* DEBUG HOST */
extern  UART_HandleTypeDef      huart3;
#define DEBUG_UART_PERIHERAL    (USART3)
#define DEBUG_IRQn               USART3_IRQn
#define DEBUG_UART_TX_PIN				GPIO_PIN_5  
#define DEBUG_UART_RX_PIN				GPIO_PIN_7  
#define DEBUG_UART_PORT			    GPIOA       
#define DEBUG_UART_PORT_AF           GPIO_AF7_USART3
#define debug_gpioclk_enable()  do{\
                                    __HAL_RCC_GPIOA_CLK_ENABLE();\
                                }while(0)
#define debug_uartclk_enable()  do{\
                                    RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};\
                                    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3;\
                                    PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;\
                                    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { \
                                        Error_Handler();\
                                    }\
                                    __HAL_RCC_USART3_CLK_ENABLE();\
                                }while(0)

/* eflash */
#define eflash_spi_ctx  (hspi1)
#define EFLASH_SPI_PERIHERAL   (SPI1)

/* gsensor */
#define gsensor_spi_ctx (hspi2)
#define GSENSOR_SPI_PERIHERAL   (SPI2)
#define gsensor_spiclk_enable()      do{\
                                        RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};\
                                        PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SPI2;\
                                        PeriphClkInitStruct.Spi2ClockSelection = RCC_SPI2CLKSOURCE_PCLK1;\
                                        if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {\
                                            Error_Handler();\
                                        }\
                                        /* Peripheral clock enable */\
                                        __HAL_RCC_SPI2_CLK_ENABLE();\
                                        __HAL_RCC_GPIOA_CLK_ENABLE();\
                                        __HAL_RCC_GPIOB_CLK_ENABLE();\
                                        }while(0)

#define gsensor_spiclk_disable()    do{\
                                        __HAL_RCC_SPI2_CLK_DISABLE();\
                                        }while(0)                            
#define GSENSOR_PIN_SCK        GPIO_PIN_13
#define GSENSOR_PIN_MISO       GPIO_PIN_14
#define GSENSOR_PIN_MOSI       GPIO_PIN_15
#define GSENSOR_PORT            GPIOB
#define GSENSOR_PIN_NSS        GPIO_PIN_12
#define GSENSOR_PORT_NSS        GPIOB
#define GSENSOR_IRQn            SPI2_IRQn      
#define GSENSOR_PIN_INT         GPIO_PIN_4
#define GSENSOR_PORT_INT        GPIOA
#define GSENSOR_PIN_PWR         GPIO_PIN_2
#define GSENSOR_PORT_PWR        GPIOB
#define gsensor_irq_handler     SPI2_IRQHandler


/* gnss */
#define gnss_uart_id            (1)
#define gnss_uart_ctx          (huart1)     /* DEBUG HOST */
extern  UART_HandleTypeDef     gnss_uart_ctx;
#define GNSS_UART_PERIHERAL    (USART1)
#define GNSS_IRQn              USART1_IRQn
#define gnss_gpioclk_enable()      do{\
                                        __HAL_RCC_GPIOA_CLK_ENABLE();\
                                        }while(0)
#define gnss_uartclk_enable()       do{\
                                        RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};\
                                        PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1;\
                                        PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;\
                                        if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {\
                                            Error_Handler();\
                                        }\
                                        __HAL_RCC_USART1_CLK_ENABLE();\
                                    }while(0)                                             
#define GNSS_UART_TX_PIN				GPIO_PIN_9  
#define GNSS_UART_RX_PIN				GPIO_PIN_10  
#define GNSS_UART_PORT_AF           GPIO_AF7_USART1
#define GNSS_UART_PORT			    GPIOA              

#define GNSS_WAKEUP_PORT            GPIOA   		/* MCU 唤醒 GPS */
#define GNSS_WAKEUP_PIN             GPIO_PIN_11   	/* MCU 唤醒 GPS */

#define GNSS_PWR_PORT               GPIOA  			/* 配置为推挽输出，GPS模块电源使能：0-关闭，1-开启. */
#define GNSS_PWR_PIN                GPIO_PIN_8	  	/* 配置为推挽输出，GPS模块电源使能：0-关闭，1-开启. */

/* modem */
#define MDM_PWR_CTRL_PIN       	 	GPIO_PIN_12  /* 配置为推挽输出，不开上下拉,输出高电平打开LTE电源. */
#define MDM_PWR_CTRL_PORT           GPIOA

#define MDM_PWR_ON_PIN          	GPIO_PIN_8  /* 配置为推挽输出，不开上下拉,输出持续高电平大于500ms模块开机, 注意硬件有做反转，应用设计需要注意 */
#define MDM_PWR_ON_PORT          	GPIOB

#define MDM_DTR_PIN			    	GPIO_PIN_9  /* 配置为推挽输出，1-空闲，0-MCU唤醒4G模块 */
#define MDM_DTR_PORT		    	GPIOB 

#define MDM_RING_PIN				GPIO_PIN_7  /* 配置为浮空输入，0-有效，1-空闲，4G模块唤醒MCU */
#define MDM_RING_PORT				GPIOB 

#define MDM_RST_PIN				    GPIO_PIN_6  /* 配置为上拉输出，0-有效，1-空闲，模块复位4G模组 */
#define MDM_RST_PORT			    GPIOB


#define MDM_UART_TX_PIN				GPIO_PIN_2  
#define MDM_UART_RX_PIN				GPIO_PIN_3  
#define MDM_UART_PORT			    GPIOA

#define modem_uart_id               (2)
#define modem_uart_ctx              (huart2)      /* Modem串口句柄 */
extern  UART_HandleTypeDef          modem_uart_ctx;
#define MODEM_UART_PERIHERAL        (USART2)
#define modem_gpioclk_enable()      do{\
                                        __HAL_RCC_GPIOA_CLK_ENABLE();\
                                        __HAL_RCC_GPIOB_CLK_ENABLE();\
                                        }while(0)
#define modem_uartclk_enable()      do{\
                                        /** Initializes the peripherals clock\
                                         */\
                                        RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};\
                                        PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART2;\
                                        PeriphClkInitStruct.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;\
                                        if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK){\
                                            Error_Handler();\
                                        }\
                                        __HAL_RCC_USART2_CLK_ENABLE();\
                                    }while(0)
#define MODEM_IRQn                  USART2_IRQn
#define MDM_UART_BAUDRATE           115200
#define MDM_UART_TX_PIN             GPIO_PIN_2
#define MDM_UART_TX_PORT            GPIOA
#define MDM_UART_RX_PIN             GPIO_PIN_3
#define MDM_UART_RX_PORT            GPIOA
#define MDM_UART_PORT_AF            GPIO_AF7_USART2
#define MDM_UART_TXBUF_SIZE         1600    /* 发送缓存大小, 改为1后不可以使用中断发送方式进行写操作. by Hubert.Lee */
#define MDM_UART_RXBUF_SIZE         1600    /* 接收缓存大小 */

/* hardware version adc */
#define ADC_VER                    (ADC1)
#define ADC_VER_CHANNEL            (ADC_CHANNEL_15) 
#define ADC_VER_CLKSOURCE          (RCC_ADCDACCLKSOURCE_HSI)
#define ADC_VER_PIN                (GPIO_PIN_0)
#define ADC_VER_PORT               (GPIOB) 
#define ADC_VER_CLK_ENABLE()        __HAL_RCC_ADC12_CLK_ENABLE()
#define ADC_VER_CLK_DISABLE()       __HAL_RCC_ADC12_CLK_DISABLE()
#define ADC_VER_GPIO_CLK_ENABLE()        __HAL_RCC_GPIOB_CLK_ENABLE()

/* external flash */
#define EXT_FLASH_POWER_PORT    (GPIOB)
#define EXT_FLASH_POWER_PIN     (GPIO_PIN_10)
#define bsp_eflash_clock_enable()  __HAL_RCC_GPIOB_CLK_ENABLE()
#define bsp_eflash_poweron()       HAL_GPIO_WritePin(EXT_FLASH_POWER_PORT, EXT_FLASH_POWER_PIN, GPIO_PIN_RESET);
#define bsp_eflash_poweroff()      HAL_GPIO_WritePin(EXT_FLASH_POWER_PORT, EXT_FLASH_POWER_PIN, GPIO_PIN_SET);

extern RTC_HandleTypeDef hrtc;
#endif

#ifdef STM32H563xx
#include "stm32h563xx.h"
#include "stm32h5xx_hal.h"
#include "stm32h5xx_hal_uart.h"
#include "stm32h5xx_hal_usart.h"
#include "stm32h5xx_hal_uart_ex.h"
#include "stm32h5xx_hal_usart_ex.h"
#include "stm32h5xx_hal_gpio.h"

#include "stm32h5xx_hal_flash.h"
#include "stm32h5xx_hal_flash_ex.h"

#include "stm32h5xx_hal_spi_ex.h"
#include "stm32h5xx_hal_spi.h"
#include "stm32h5xx_hal_def.h"
#include "stm32h5xx_hal_rcc.h"
#include "stm32h5xx_hal_iwdg.h"
#include "stm32h5xx_hal_adc.h"



/* debug port */
#define debug_uart_id               (1)
#define debug_uart_ctx          (huart1)     /* DEBUG HOST */
extern  UART_HandleTypeDef      huart1;
#define DEBUG_UART_PERIHERAL    (USART1)
#define DEBUG_IRQn               USART1_IRQn
#define DEBUG_UART_TX_PIN				GPIO_PIN_9  
#define DEBUG_UART_RX_PIN				GPIO_PIN_10  
#define DEBUG_UART_PORT			    GPIOA     
#define DEBUG_UART_PORT_AF           GPIO_AF7_USART1
#define debug_uartclk_enable()  do{\
                                        RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};\
                                        PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1;\
                                        PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;\
                                        if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {\
                                            Error_Handler();\
                                        }\
                                        /* Peripheral clock enable */\
                                        __HAL_RCC_USART1_CLK_ENABLE();\
                                    }while(0)
        
#define debug_gpioclk_enable()  do{\
                                        __HAL_RCC_GPIOA_CLK_ENABLE();\
                                    }while(0)


/* gsensor */
#define gsensor_spi_ctx (hspi3)
#define GSENSOR_SPI_PERIHERAL   (SPI3)
#define gsensor_spiclk_enable()      do{\
                                        RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};\
                                        PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SPI3;\
                                        PeriphClkInitStruct.Spi3ClockSelection = RCC_SPI3CLKSOURCE_PLL1Q;\
                                        if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {\
                                            Error_Handler();\
                                        }\
                                        /* Peripheral clock enable */\
                                        __HAL_RCC_SPI3_CLK_ENABLE();\
                                        __HAL_RCC_GPIOC_CLK_ENABLE();\
                                        __HAL_RCC_GPIOA_CLK_ENABLE();\
                                        }while(0)
#define gsensor_spiclk_disable()    do{\
                                        __HAL_RCC_SPI3_CLK_DISABLE();\
                                        }while(0)                                             
#define GSENSOR_PIN_SCK        GPIO_PIN_10
#define GSENSOR_PIN_MISO       GPIO_PIN_11
#define GSENSOR_PIN_MOSI       GPIO_PIN_12
#define GSENSOR_PORT            GPIOC
#define GSENSOR_PIN_NSS        GPIO_PIN_4
#define GSENSOR_PORT_NSS        GPIOA
#define GSENSOR_IRQn            SPI3_IRQn    
#define GSENSOR_PIN_INT         GPIO_PIN_9
#define GSENSOR_PORT_INT        GPIOC
#define GSENSOR_PIN_PWR         GPIO_PIN_8
#define GSENSOR_PORT_PWR        GPIOC
#define gsensor_irq_handler     SPI3_IRQHandler

/* gnss */
#define gnss_uart_id               (6)
#define gnss_uart_ctx          (huart6)     /* GNSS UART HOST */
extern  UART_HandleTypeDef     gnss_uart_ctx;
#define GNSS_UART_PERIHERAL    (USART6)
#define GNSS_IRQn              USART6_IRQn
#define gnss_gpioclk_enable()      do{\
                                        __HAL_RCC_GPIOA_CLK_ENABLE();\
                                        __HAL_RCC_GPIOC_CLK_ENABLE();\
                                        __HAL_RCC_GPIOG_CLK_ENABLE();\
                                    }while(0)
#define gnss_uartclk_enable()       do{\
                                        RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};\
                                        PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART6;\
                                        PeriphClkInitStruct.Usart6ClockSelection = RCC_USART6CLKSOURCE_PCLK2;\
                                        if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {\
                                            Error_Handler();\
                                        }\
                                        __HAL_RCC_USART6_CLK_ENABLE();\
                                    }while(0)                                        
#define GNSS_UART_TX_PIN				GPIO_PIN_6  
#define GNSS_UART_RX_PIN				GPIO_PIN_7  
#define GNSS_UART_PORT_AF           GPIO_AF7_USART6
#define GNSS_UART_PORT			    GPIOC        

#define GNSS_WAKEUP_PORT            GPIOB   		/* MCU 唤醒 GPS */
#define GNSS_WAKEUP_PIN             GPIO_PIN_1   	/* MCU 唤醒 GPS */

#define GNSS_PWR_PORT               GPIOG  			/* 配置为推挽输出，GPS模块电源使能：0-关闭，1-开启. */
#define GNSS_PWR_PIN                GPIO_PIN_7	  	/* 配置为推挽输出，GPS模块电源使能：0-关闭，1-开启. */

/* modem */
#define MDM_PWR_CTRL_PIN       	 	GPIO_PIN_2  /* 配置为推挽输出，不开上下拉,输出高电平打开LTE电源. */
#define MDM_PWR_CTRL_PORT           GPIOI

#define MDM_PWR_ON_PIN          	GPIO_PIN_15  /* 配置为推挽输出，不开上下拉,输出持续高电平大于500ms模块开机, 注意硬件有做反转，应用设计需要注意 */
#define MDM_PWR_ON_PORT          	GPIOA

#define MDM_DTR_PIN			    	GPIO_PIN_15  /* 配置为推挽输出，1-空闲，0-MCU唤醒4G模块 */
#define MDM_DTR_PORT		    	GPIOH 

#define MDM_RING_PIN				GPIO_PIN_2  /* 配置为浮空输入，0-有效，1-空闲，4G模块唤醒MCU */
#define MDM_RING_PORT				GPIOD 

#define MDM_RST_PIN				    GPIO_PIN_2  /* 配置为上拉输出，0-有效，1-空闲，模块复位4G模组 */
#define MDM_RST_PORT			    GPIOH

#define MDM_UART_TX_PIN				GPIO_PIN_5 
#define MDM_UART_RX_PIN				GPIO_PIN_6  
#define MDM_UART_PORT			    GPIOD

#define modem_uart_id               (2)
#define modem_uart_ctx              (huart2)      /* Modem串口句柄 */
extern  UART_HandleTypeDef          modem_uart_ctx;
#define MODEM_UART_PERIHERAL        (USART2)
#define modem_gpioclk_enable()       do{\
                                        __HAL_RCC_GPIOI_CLK_ENABLE();\
                                        __HAL_RCC_GPIOD_CLK_ENABLE();\
                                        __HAL_RCC_GPIOH_CLK_ENABLE();\
                                        __HAL_RCC_GPIOA_CLK_ENABLE();\
                                        }while(0)
#define modem_uartclk_enable()      do{\
                                        /** Initializes the peripherals clock\
                                         */\
                                        RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};\
                                        PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART2;\
                                        PeriphClkInitStruct.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;\
                                        if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK){\
                                            Error_Handler();\
                                        }\
                                        __HAL_RCC_USART2_CLK_ENABLE();\
                                    }while(0)
#define MODEM_IRQn                  USART2_IRQn
#define MDM_UART_BAUDRATE           115200
#define MDM_UART_TX_PIN             GPIO_PIN_5
#define MDM_UART_TX_PORT            GPIOD
#define MDM_UART_RX_PIN             GPIO_PIN_6
#define MDM_UART_RX_PORT            GPIOD
#define MDM_UART_PORT_AF            GPIO_AF7_USART2
#define MDM_UART_TXBUF_SIZE         1600    /* 发送缓存大小, 改为1后不可以使用中断发送方式进行写操作. by Hubert.Lee */
#define MDM_UART_RXBUF_SIZE         1600    /* 接收缓存大小 */

/* hardware version adc */
#define ADC_VER                    (ADC1)
#define ADC_VER_CHANNEL            (ADC_CHANNEL_15) 
#define ADC_VER_CLKSOURCE          (RCC_ADCDACCLKSOURCE_HCLK)
#define ADC_VER_PIN                (GPIO_PIN_3)
#define ADC_VER_PORT               (GPIOA) 
#define ADC_VER_CLK_ENABLE()        __HAL_RCC_ADC_CLK_ENABLE()
#define ADC_VER_CLK_DISABLE()       __HAL_RCC_ADC_CLK_DISABLE()
#define ADC_VER_GPIO_CLK_ENABLE()        __HAL_RCC_GPIOA_CLK_ENABLE()

/* external flash */
#define EXT_FLASH_POWER_PORT    (GPIOB)
#define EXT_FLASH_POWER_PIN     (GPIO_PIN_0)
#define bsp_eflash_clock_enable()  __HAL_RCC_GPIOB_CLK_ENABLE();
#define bsp_eflash_poweron()       HAL_GPIO_WritePin(EXT_FLASH_POWER_PORT, EXT_FLASH_POWER_PIN, GPIO_PIN_SET);
#define bsp_eflash_poweroff()      HAL_GPIO_WritePin(EXT_FLASH_POWER_PORT, EXT_FLASH_POWER_PIN, GPIO_PIN_RESET);
#define eflash_irq_handler         SPI1_IRQHandler
#include "stm32h5xx_hal_cortex.h"
#endif

#ifdef __cplusplus
}
#endif

/*@}*/

#endif
